package com.util;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.SystemColor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.UIManager;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableRowSorter;

import com.client.ctrl.RoatCtrl;
import com.entity.User;

public class MyTable2 extends JTable  implements MouseListener{
/**
	 * 
	 */
	private static final long serialVersionUID = -8333955997519075698L;
	private RoatCtrl roatCtrl=new RoatCtrl();
	private String tableName;
	final JLabel label=new JLabel();
    private User user;
	
	
	public MyTable2(final Vector<Vector<Object>> dataValues,final Vector<String> columnNames,final int[] width,String tableName,User user){
    
	this.user=user;	
	this.tableName=tableName;
    final MyTableModel tableModel=new MyTableModel(dataValues, columnNames);
	setModel(tableModel);
	this.setCursor(new Cursor(Cursor.HAND_CURSOR));
	this.setRowSorter(new TableRowSorter<MyTableModel>(tableModel));
   // setShowHorizontalLines(false);
    //setShowVerticalLines(false);
    setRowHeight(30);
    setEnabled(false);
    final JCheckBox selectAllBox=new JCheckBox(tableModel.getColumnName(0));
    final JTableHeader tableHeader=getTableHeader();
    tableHeader.setPreferredSize(new Dimension(300, 40));
    tableHeader.setBackground(new Color(139,171,242));
    CommonUtil.setFont(tableHeader, 1);
    
    tableHeader.setDefaultRenderer(new TableCellRenderer() {
		
		public Component getTableCellRendererComponent(JTable table, Object value,
				boolean isSelected, boolean hasFocus, int row, int column) {
		String valueStr=(String)value;
		JLabel label=new JLabel(valueStr);
		label.setHorizontalAlignment(JLabel.CENTER);
		selectAllBox.setHorizontalAlignment(JLabel.CENTER);
		selectAllBox.setBorderPainted(true);
		Component component=null;
		if(column==0){
			component= selectAllBox;
		}
		else{
			component= label;
		}
		component.setForeground(tableHeader.getForeground());
		component.setBackground(tableHeader.getBackground());
		component.setFont(tableHeader.getFont());
		((JComponent) component).setBorder(UIManager.getBorder("TableHeader.cellBorder"));
		
		return component;
		}
	});
    
    this.setCursor(new Cursor(Cursor.HAND_CURSOR));
    getColumnModel().getColumn(1).setCellRenderer(new TableCellRenderer() {
		public Component getTableCellRendererComponent(JTable table, Object value,
				boolean isSelected, boolean hasFocus, int row, int column) {
			String valueStr=(String)value;
			label.setText(valueStr);
			label.setHorizontalAlignment(JLabel.CENTER);
			label.setForeground(Color.blue);
			label.setBackground(table.getBackground());
			label.setFont(table.getFont());
			return label;
		}
	});
    for( int i=0;i<getColumnCount();i++){
    	getColumnModel().getColumn(i).setPreferredWidth(width[i]);
    }
    setBackground(SystemColor.window);
    setForeground(SystemColor.textInactiveText);	
    CommonUtil.setFont(this,5);
    
    
       this.addMouseListener(this);
      tableHeader.addMouseListener(new MouseListener() {
		
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		public void mouseClicked(MouseEvent e) {
			int selectColumn=tableHeader.columnAtPoint(e.getPoint());
			if(selectColumn==0){
				selectAllBox.setSelected(!selectAllBox.isSelected());
				for(int i=0;i<dataValues.size();i++){
					tableModel.setValueAt(selectAllBox.isSelected(), i, selectColumn);
				}
				
			};
		}
	});
    
}

public void mouseClicked(MouseEvent e) {
	final int selectcolumn=this.columnAtPoint(e.getPoint());
	final int selectrow=this.rowAtPoint(e.getPoint());
	if(selectcolumn==0){
	    Boolean a=(Boolean)this.getValueAt(selectrow, selectcolumn);
	    this.setValueAt(!a,selectrow, selectcolumn);
	}else if(selectcolumn==1){
		String title=(((String)this.getModel().getValueAt(selectrow,1)).split("<html><i>")[1]).split("</i></html>")[0];
		    if(tableName.equals("公告表")){
			  roatCtrl.updateAnnouncement(this, title,user);
			}
	}
}
public void mousePressed(MouseEvent e) {
	
	
}

public void mouseReleased(MouseEvent e) {
	// TODO Auto-generated method stub
	
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}


}
